#<#
# Verifier Script - This script confirms that a root cause has been resolved correctly
# Key Cmdlets:
# -- update-diagrootcause flags the status of a root cause and can be used to pass parameters
# -- get-diaginput invokes an interactions and returns the response
# -- write-diagprogress displays a progress string to the user
##>

param ($id, $name,$xmlErr)

. ./FunctionsLib.ps1

Function Verify {

	Import-LocalizedData -BindingVariable Strings_VF -FileName LocalizationData
	write-diagprogress ($Strings_VF.ID_PROG_VERIFY).Replace("%SettingsGroup%",$name) -Status $Strings_VF.ID_PROG_VERIFY_STATUS
	sleep -Milliseconds 500

	if (Test-Path "Settings.xml") 
	{	
		# Load XML from disk
		[xml]$xml = Get-Content "Settings.xml" -Force

		# Get the settings Group from XML that matches the one that the user selected via interaction
		$group = $xml.selectSingleNode("/SoftwareReset/SettingsGroup[@id='$id']")
	      
		# Processing session group
		if($group -ne $null)
		{	   
		# Get a collection of settings to verify
		$Settings = $Group.SelectNodes("Settings/*")  

		# Iterate over settings and verify the changes
		[int]$failedTasks = 0
		foreach ($Setting in $Settings)
		{ 
		    # Determine proper handler for this setting and pass it along
		    switch ($Setting.Name)
		    {  
		        "SetRegValue"   
				{ 		
					
					if ((verify_SetRegValue $Setting) -ne "fixed")
					{    
						$failedTasks ++				
					}
					
				}
				
		        "DeleteRegValue" 
				{    
					if ((verify_DeleteRegValue $Setting) -ne "fixed")
					{    
						$failedTasks ++					 
					}
				}
		        "RenameRegValue" 
				{ 
					if ((verify_RenameRegValue $Setting) -ne "fixed")
					{    
						$failedTasks ++					
					}
				}
		        "CopyFile"       
				{
					if ((verify_CopyFile $Setting) -ne "fixed")
					{    
						$failedTasks ++					
					}
				}
		        "DeleteFile"     
				{ 
					if ((verify_DeleteFile $Setting) -ne "fixed")
					{    
						$failedTasks ++				
					}
				}			
		        
			}		
			
		} # end of foreach

		if ($failedTasks -ne 0)
		{  
		$RootCauseDetected = $True   
		}  
		else
		{
		$RootCauseDetected = $false
		} 
		  
	  Update-DiagRootCause -Id "RC_AppSettings" -Detected $RootCauseDetected -InstanceId $id 	 
	  
	  # Add the RC instance ID (settings Group ID) and its status to the hash table
	  $Global:Verified.add($id , $RootCauseDetected)  
	  
	 }
   }
} # end of Function:Verify


# Determine whether the RC instance (settings group ID) being verified matches 
# the one for which the resolver just ran
if ($Global:RS_Run_ID -eq $id){
   # Step A: Is the right Verifier. Perform the verification 
   Verify
}

else
{ # Verifier doesn't match the resolver
   
	# Determine whether the RC instance (settings group ID) being verified belongs 
	# to those for which the resolver already ran (and hence already verified at Step A)
	if($Global:RS_alreadyRun -contains $id){    
	
	# Step B: Already verified. Get the status from the cache at Step A and update the RC accordingly
	$detected = $Global:Verified.get_Item($id)
	Update-DiagRootCause -Id "RC_AppSettings" -Detected $detected -InstanceId $id 
	}
	
	else {	
	# Step B: RS not yet run. Update the RC to "True"
	Update-DiagRootCause -Id "RC_AppSettings" -Detected $true -InstanceId $id	
	}
}

